#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"

#pragma comment(linker, \
	"\"/manifestdependency:type='Win32' "\
	"name='Microsoft.Windows.Common-Controls' "\
	"version='6.0.0.0' "\
	"processorArchitecture='*' "\
	"publicKeyToken='6595b64144ccf1df' "\
	"language='*'\"")

#pragma comment(lib, "ComCtl32.lib")

HINSTANCE hInst;
HWND hDlg_PIN;
HWND hDlg_Card;
HWND hDlg_Menu;
HWND hDlg_ViewBalance;
HWND hDlg_WithdrawMoney;
HWND hDlg_Other;
INT_PTR CALLBACK DialogProc_AuthUser (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc_ValidateCard(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc_Menu (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc_ViewBalance (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc_WithdrawMoney (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc_Other (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);


INT_PTR CALLBACK DialogProc_ValidateCard(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDCANCEL:
			SendMessage(hDlg, WM_CLOSE, 0, 0);
			return TRUE;
		case IDOK:
			//Validate CardID
			InitCommonControls();			
			hDlg_PIN = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_AuthUser), 0, DialogProc_AuthUser, 0);
			ShowWindow(hDlg_PIN, SW_NORMAL);
			ShowWindow(hDlg_Card, SW_HIDE);			
		}
		break;		

	case WM_CLOSE:
		if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
			MB_ICONQUESTION | MB_YESNO) == IDYES)
		{
			DestroyWindow(hDlg);
		}
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}

INT_PTR CALLBACK DialogProc_AuthUser (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDCANCEL:
			ShowWindow(hDlg_Card, SW_NORMAL);
			ShowWindow(hDlg_PIN, SW_HIDE);
			SetFocus(hDlg_Card);	
			return TRUE;	
		case IDOK:
			hDlg_Menu=CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_Menu), 0, DialogProc_Menu, 0);
			ShowWindow(hDlg_Menu,SW_NORMAL);
			ShowWindow(hDlg_PIN,SW_HIDE);
			SetFocus(hDlg_Menu);
		}
		break;		

	case WM_CLOSE:
		ShowWindow(hDlg_Card, SW_NORMAL);
		ShowWindow(hDlg_PIN, SW_HIDE);
		SetFocus(hDlg_Card);
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}

INT_PTR CALLBACK DialogProc_Menu (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{		
		case IDC_BUTTON_ViewBalance:
			hDlg_ViewBalance=CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_ViewBalance), 0, DialogProc_ViewBalance, 0);
			ShowWindow(hDlg_ViewBalance,SW_NORMAL);
			ShowWindow(hDlg_Menu,SW_HIDE);
			SetFocus(hDlg_ViewBalance);
			return TRUE;
		case IDC_BUTTON_WithDrawMoney:
			hDlg_WithdrawMoney = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_WithDrawMoney), 0, DialogProc_WithdrawMoney, 0);
			ShowWindow(hDlg_WithdrawMoney,SW_NORMAL);
			ShowWindow(hDlg_Menu,SW_HIDE);
			SetFocus(hDlg_WithdrawMoney);
			return TRUE;
		}
		break;		

	case WM_CLOSE:
		ShowWindow(hDlg_Card, SW_NORMAL);
		//ShowWindow(hDlg_PIN, SW_HIDE);
		ShowWindow(hDlg_Menu,SW_HIDE);
		SetFocus(hDlg_Card);
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}



INT_PTR CALLBACK DialogProc_ViewBalance (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{		
		case IDOK:		
			int MsgBoxID=MessageBox(hDlg_ViewBalance,TEXT("Do you want to print the receipt?"),TEXT("Print receipt"),MB_YESNO|MB_ICONQUESTION);
			//switch (MsgBoxID)
			//{
			//case IDYES:
			//	//Print the receipt;
			//}
			MsgBoxID=MessageBox(hDlg_ViewBalance,TEXT("Do you want to continue other transaction?"),TEXT("Confirm"), MB_YESNO|MB_ICONQUESTION);
			switch (MsgBoxID)
			{
			case IDYES:
				ShowWindow(hDlg_Menu,SW_NORMAL);
				ShowWindow(hDlg_ViewBalance,SW_HIDE);
				SetFocus(hDlg_Menu);
				break;
			case IDNO:
				ShowWindow(hDlg_Card,SW_NORMAL);
				ShowWindow(hDlg_ViewBalance,SW_HIDE);
				SetFocus(hDlg_Card);
				break;
			}
			//*/*ShowWindow(hDlg_Menu, SW_NORMAL);
			//ShowWindow(hDlg_ViewBalance, SW_HIDE);
			//SetFocus(hDlg_Menu);*/*/
			return TRUE;
		}
		break;		

	case WM_CLOSE:
		if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
			MB_ICONQUESTION | MB_YESNO) == IDYES)
		{
			DestroyWindow(hDlg);
		}
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}



INT_PTR CALLBACK DialogProc_WithdrawMoney (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{		
		case IDC_BUTTON_Other:
			hDlg_Other=CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_Other), 0, DialogProc_Other, 0);
			ShowWindow(hDlg_Other, SW_NORMAL);
			ShowWindow(hDlg_WithdrawMoney, SW_HIDE);	
			SetFocus(hDlg_Other);
			return TRUE;
		}
		break;		

	case WM_CLOSE:
		if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
			MB_ICONQUESTION | MB_YESNO) == IDYES)
		{
			DestroyWindow(hDlg);
		}
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}

INT_PTR CALLBACK DialogProc_Other(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{		
		case IDC_BUTTON_Other:
			hDlg_Other=CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_Other), 0, DialogProc_Other, 0);
			ShowWindow(hDlg_Other, SW_NORMAL);
			ShowWindow(hDlg_WithdrawMoney, SW_HIDE);	
			SetFocus(hDlg_Other);
			return TRUE;
		}
		break;		

	case WM_CLOSE:
		if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
			MB_ICONQUESTION | MB_YESNO) == IDYES)
		{
			DestroyWindow(hDlg);
		}
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}

int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE h0, LPTSTR lpCmdLine, int nCmdShow)
{	
	MSG msg;
	BOOL ret;

	InitCommonControls();
	hDlg_Card = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_ValidateCard), 0, DialogProc_ValidateCard, 0);
	ShowWindow(hDlg_Card, nCmdShow);

	while((ret = GetMessage(&msg, 0, 0, 0)) != 0) {
		if(ret == -1)
			return -1;

		if(!IsDialogMessage(hDlg_Card, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return 0;
}

